package com.bupt.cosmfl.cosmflweb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bupt.cosmfl.cosmflweb.constant.Path;
import com.bupt.cosmfl.cosmflweb.constant.ResponseResult;
import com.bupt.cosmfl.cosmflweb.dto.NodeRegisterDTO;
import com.bupt.cosmfl.cosmflweb.dto.PageDTO;
import com.bupt.cosmfl.cosmflweb.model.NodeEntity;
import com.bupt.cosmfl.cosmflweb.service.NodeManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author fangtiewei
 * @Date 2021/7/28 16:22
 * @Description
 */
@RestController
@RequestMapping("/nodeManage")
public class NodeManageController {

    @Autowired
    private NodeManageService nodeManageService;

    @GetMapping(Path.NODE_LIST)
    public ResponseResult<Object> list(PageDTO dto){
        Map<String,Object> res = new HashMap<>();
        IPage<NodeEntity> nodeListPage = nodeManageService.list(dto);
        res.put("nodeList",nodeListPage.getRecords());
        res.put("total",nodeListPage.getTotal());
        return ResponseResult.ok(res);

    }

    @PostMapping(Path.NODE_CREATE)
    public ResponseResult<Map<String, Integer>> register(@RequestBody NodeRegisterDTO dto) {
        Map<String, Integer> res = new HashMap<>();
        Integer insertNum = nodeManageService.registerNode(dto);
        res.put("insertNum", insertNum);
        return ResponseResult.ok(res);
    }

    @PostMapping(Path.NODE_DELETE)
    public ResponseResult<Object> deleteNode(@PathVariable @Valid Long id) {
        nodeManageService.deleteNode(id);
        return ResponseResult.ok(null, "删除成功");
    }
}
